/*
 * @Author: szx
 * @Date: 2021-11-12 12:45:59
 * @LastEditTime: 2021-11-12 12:58:39
 * @Description:
 * @FilePath: \leetcode\400-499\438\438.cpp
 */
#include <iostream>
#include <vector>
using namespace std;
class Solution {
   public:
    vector<int> findAnagrams(string s, string p) {
        int m = s.length(), n = p.length();
        vector<int> res;
        if (m < n) return res;
        vector<int> a(26, 0);
        for (char ch : p) {
            a[ch - 'a']++;
        }
        vector<int> b(26, 0);
        for (int i = 0; i < n - 1; i++) {
            b[s[i] - 'a']++;
        }
        for (int i = 0; i < m - n + 1; i++) {
            b[s[i + n - 1] - 'a']++;
            if (b == a) {
                res.push_back(i);
            }
            b[s[i] - 'a']--;
        }
        return res;
    }
};